import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.text.Normalizer;

class XorEncoder {
	public static void xorFile(String inF, String outF, String key) throws IOException {
		RandomAccessFile raf = new RandomAccessFile(inF, "r");
		int dataLen=(int)raf.length(), keyMaxLen=key.length()-1, pK=0;
		byte[] rawData = new byte[dataLen];
		byte[] rawKey = key.getBytes();
		byte[] rawRes = new byte[dataLen];
		raf.read(rawData, 0, dataLen);
		raf.close();
		for (int i=0; i<dataLen; i++) {
			rawRes[i] = (byte)(rawData[i] ^ rawKey[pK]);
			pK = (pK==keyMaxLen ? 0 : pK+1);
		}
		RandomAccessFile raf2 = new RandomAccessFile(outF, "rw");
		raf2.write(rawRes);
		raf2.close();
	}

	public static void main(String[] arg) {
		try {
			xorFile("out.txt", "encrypted.dat", "hello");
			xorFile("encrypted.dat", "decrypted.txt", "hello");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
